from django.db import models
# 时间模块快
from django.utils import timezone
# 用户表
from django.contrib.auth.models import AbstractUser


# Create your models here.

# 基类表
class Base(models.Model):
    create_time = models.DateTimeField(default=timezone.now, null=True)  # 添加数据时，自动写入当前添加时间
    is_delete = models.IntegerField(default=0)  # 逻辑关联删除字段({0:未删除,1:已删除})

    class Meta:
        abstract = True


# 用户
# 用户和角色是多对多
class User(AbstractUser):
    username = models.CharField(max_length=200, unique=True)  # 用户名
    password = models.CharField(max_length=200)  # 密码
    phone = models.CharField(max_length=11)  # 手机号
    photo = models.CharField(max_length=200, default="1.jpg")  # 头像
    office = models.ForeignKey('Office', on_delete=models.SET_NULL, null=True)  # 公司id
    is_delete = models.IntegerField(default=0)  # 逻辑删除
    character = models.ManyToManyField('Character')

    class Meta:
        db_table = "user"


# 角色
# 角色和用户是多对多
# 角色和权限是多对多
# 公司和角色是一对多，每个公司可以根据需要定制自己的角色，公司之间可以重名，但是内部不可以，需要建立联合唯一索引
class Character(models.Model):
    character = models.CharField(max_length=100)
    office = models.ForeignKey('Office', on_delete=models.SET, null=True)
    right = models.ManyToManyField('Right')

    class Meta:
        db_table = 'character'
        unique_together = (
            ('office', 'character'),
        )


# 权限
# 权限和角色是多对多
class Right(models.Model):
    url = models.CharField(max_length=100, null=True)
    modal = models.CharField(max_length=100, null=True)
    desc = models.CharField(max_length=100, null=True)
    givable = models.IntegerField(default=1)

    class Meta:
        db_table = 'right'


# 公司
# 公司和用户是一对多
class Office(Base):
    office = models.CharField(max_length=200)
    city = models.CharField(max_length=100)
    street = models.CharField(max_length=200)
    img = models.CharField(max_length=200)

    class Meta:
        db_table = 'office'


# 设备
class Equipment(Base):
    equipment = models.CharField(max_length=60, unique=True)
    desc = models.CharField(max_length=300)

    class Meta:
        db_table = 'equipment'


# 货物类型
# 货物类型和公司一对多
class Type(Base):
    type = models.CharField(max_length=60)
    delay = models.DecimalField(max_digits=5, decimal_places=1, )
    desc = models.CharField(max_length=300, null=True)
    period = models.DecimalField(max_digits=5, decimal_places=1, )
    low_temp = models.DecimalField(max_digits=5, decimal_places=1, )
    high_temp = models.DecimalField(max_digits=5, decimal_places=1, )
    office = models.ForeignKey(Office, on_delete=models.SET_NULL, null=True)

    class Meta:
        db_table = 'type'


# 报警
class Alert(Base):
    alert = models.CharField(max_length=60)
    type = models.ForeignKey(Type, on_delete=models.SET)
    low_alert = models.DecimalField(max_digits=5, decimal_places=1, null=True)
    high_alert = models.DecimalField(max_digits=5, decimal_places=1, null=True)
    threshold = models.IntegerField()

    class Meta:
        db_table = 'alert'
        unique_together = (
            ('alert', 'type'),
        )


# 运单
class Order(Base):
    order = models.CharField(max_length=120, unique=True)
    equipment = models.ForeignKey(Equipment, on_delete=models.SET)
    type = models.ManyToManyField(Type)
    low_temp = models.DecimalField(max_digits=5, decimal_places=1, )
    high_temp = models.DecimalField(max_digits=5, decimal_places=1, )
    # period只需读取第一个货物类型的周期即可，周期不同不能生成运单
    # delay只需读取第一个货物类型的延时即可，周期不同不能生成运单
    start_time = models.DateTimeField(null=True)
    end_time = models.DateTimeField(null=True)
    result = models.CharField(max_length=10, null=True)     # 未下单，在途，成交，返货
    fee = models.IntegerField(null=True)
    is_paid = models.IntegerField(default=0)
    order_id = models.CharField(max_length=120, null=True)
    custom = models.ForeignKey('Custom', on_delete=models.SET_NULL, null=True)
    origin = models.CharField(max_length=200, null=True)    # 不填默认客户的公司地址
    start_location = models.CharField(max_length=100, null=True)
    end_location = models.CharField(max_length=100, null=True)
    report = models.TextField(null=True)
    # 关联区域信息
    ordered_region = models.ForeignKey('OrderedRegion', on_delete=models.SET_NULL, null=True)

    class Meta:
        db_table = 'order'


# 客户
class Custom(Base):
    custom = models.CharField(max_length=120)
    address = models.CharField(max_length=300)
    phone = models.CharField(max_length=12)
    phone2 = models.CharField(max_length=12, null= True)
    name = models.CharField(max_length=100)
    office = models.ForeignKey(Office, on_delete=models.SET_NULL, null=True)

    class Meta:
        db_table = 'custom'

# 地区级别
class Region(models.Model):
    code = models.CharField(max_length=5)
    region = models.CharField(max_length=50)
    upgrade = models.CharField(max_length=5, null=True)

    class Meta:
        db_table = 'region'

# 地区运单关系
class OrderedRegion(Base):
    start = models.ForeignKey(Region,related_name='start', on_delete= models.SET)
    end = models.ForeignKey(Region,related_name='end', on_delete=models.SET)
    office = models.ForeignKey(Office, on_delete= models.SET)

    class Meta:
        db_table = 'ordered_region'